/*
 * 文件名：RoleDaoImpl.java
 * 版权：Copyright by will_awoke
 * 描述：
 * 修改人：will_awoke
 * 修改时间：2014-8-19
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.qbgs.rbac.dao.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import com.qbgs.base.Page;
import com.qbgs.rbac.dao.RoleDao;


/**
 * 
 * @author will_awoke
 * @version 2014-8-19
 * @see RoleDaoImpl
 * @since
 */
@Repository
public class RoleDaoImpl implements RoleDao
{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Page queryRolePage(int currentPage, int numPerPage, String roleName)
    {
        // 查询参数值集合
        List<Object> queryParamVals = null;

        StringBuffer sql = new StringBuffer();
        sql.append(" select role_id,role_name,remarks,create_time from t_sys_role where 1=1 ");

        if (!StringUtils.isBlank(roleName))
        {
            sql.append(" and instr(role_name,?)>0 ");
            queryParamVals = new LinkedList<Object>();
            queryParamVals.add(roleName);
        }
        sql.append(" order by role_id desc");

        return new Page(sql.toString(), queryParamVals, currentPage, numPerPage, jdbcTemplate,
            true);
    }

    @Override
    public List<Map<String, Object>> queryAllRoles()
    {
        String sql = " select role_id,role_name from t_sys_role order by role_id desc ";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public List<Map<String, Object>> queryRoleById(String roleId)
    {
        String sql = " select role_name,remarks from t_sys_role where role_id=? ";
        Object[] parms = {roleId};

        return jdbcTemplate.queryForList(sql, parms);
    }

    @Override
    public int addRole(final String roleName, final String remarks)
    {
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator()
        {
            public PreparedStatement createPreparedStatement(Connection conn)
                throws SQLException
            {
                String sql = " insert into t_sys_role(role_name,remarks,create_time) values(?,?,SYSDATE()) ";
                PreparedStatement pstat = conn.prepareStatement(sql);
                pstat.setString(1, roleName);
                pstat.setString(2, remarks);
                return pstat;
            }
        }, keyHolder);

        return keyHolder.getKey().intValue();
    }

    @Override
    public int addRoleMenu(String roleId, String menuId)
    {
        String sql = " insert into t_role_menu(role_id,menu_id) values(?,?) ";
        Object[] paras = {roleId, menuId};

        return jdbcTemplate.update(sql, paras);
    }

    @Override
    public int queryRoleRefUser(String roleId)
    {
        String sql = " select count(1) from t_user_role where role_id=? ";
        Object[] paras = {roleId};

        return jdbcTemplate.queryForInt(sql, paras);
    }

    @Override
    public List<String> queryMenuIdsByRoleId(String roleId)
    {
        String sql = " select menu_id from t_role_menu where role_id=? ";
        Object[] paras = {roleId};

        return jdbcTemplate.queryForList(sql, paras, String.class);
    }

    @Override
    public int deleteMenusOfRole(String roleId)
    {
        String sql = " delete from t_role_menu where role_id=? ";
        Object[] paras = {roleId};

        return jdbcTemplate.update(sql, paras);
    }

    @Override
    public int deleteRole(String roleId)
    {
        String sql = " delete from t_sys_role where role_id=? ";
        Object[] paras = {roleId};

        return jdbcTemplate.update(sql, paras);
    }

    @Override
    public int updateRole(String roleId, String roleName, String remarks)
    {
        String sql = " update t_sys_role set role_name=?,remarks=? where role_id=? ";
        Object[] paras = {roleName, remarks, roleId};

        return jdbcTemplate.update(sql, paras);
    }

}
